package com.smarttest.common.util.protocol;

import org.apache.commons.codec.binary.Hex;
import org.apache.commons.lang.StringUtils;

import com.smarttest.common.enums.SmartType;

public class SmartUtils {

    /**
     * 计算input数组从pos开始的校验码
     * @param input
     * @param pos
     * @return b 校验码的值
     */
    public static byte checksum(byte[] input, int pos, int limit) {
        byte b = input[pos];
        for (int i = pos + 1; i < limit; i++) {
            b ^= input[i];
        }
        return b;
    }

    /**
     * 将hex string转换为byte数组
     * @param text
     * @return bytes
     */
    public static byte[] str2bytes(String text) {
        if (StringUtils.isEmpty(text)) {
            return null;
        }
        String[] ss = text.split(" ");
        byte[] bytes = null;
        if (ss.length == 1) {
            bytes = new byte[text.length() / 2];
            for (int i = 0; i < bytes.length; i++) {
                bytes[i] = (byte) Integer.parseInt(text.substring(i * 2, i * 2 + 2), 16);
            }
        } else {
            bytes = new byte[ss.length];
            for (int i = 0; i < bytes.length; i++) {
                bytes[i] = (byte) Integer.parseInt(ss[i], 16);
            }
        }
        return bytes;
    }

    /**
     * 将bcd字节转换位整数
     * @param b
     * @return
     */
    public static int bcd(byte b) {
        return Integer.parseInt(Integer.toString(b, 16), 10);
    }

    public static short crc16(byte[] AP, int j) {
        short CRC16;
        int i;
        byte CRC_H4 = 0;
        short CRCTable[] = { 0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50A5,
                0x60C6, 0x70E7, (short) 0x8108, (short) 0x9129, (short) 0xA14A,
                (short) 0xB16B, (short) 0xC18C, (short) 0xD1AD, (short) 0xE1CE,
                (short) 0xF1EF };
        CRC16 = (short) (0xFFFF - (j & 0xFFFF));

        for (i = 0; i < AP.length; i++) {
            try {
                CRC_H4 = (byte) (CRC16 >> 12 & 0x0F);
                CRC16 = (short) (CRC16 << 4);
                CRC16 = (short) (CRC16 ^ CRCTable[CRC_H4
                        ^ AP[i] >> 4 & 0x0F]);
                CRC_H4 = (byte) (CRC16 >> 12 & 0x0F);
                CRC16 = (short) (CRC16 << 4);
                CRC16 = (short) (CRC16 ^ CRCTable[CRC_H4 ^ AP[i] & 0x0F]);
            } catch (Exception e) {
                System.out.println(CRC_H4 + "" + (AP[i] >> 4 & 0x0F));
            }
        }

        return CRC16;
    }

    /**
     * 格式化硬件版本号 xxx.xxx
     *
     * @param hdver  整数形式硬件版本号
     * @return
     */
    public static String getFormatHdVer(int hdver) {
        StringBuilder hdVerStr = new StringBuilder();
        hdVerStr.append(hdver >> 4 & 0xF);
        hdVerStr.append(".");
        hdVerStr.append(hdver & 0xF);

        return hdVerStr.toString();
    }

    /**
     * 获取版本号
     *
     * @param data smart消息体
     * @return
     */
    public static String getFormatSoftVer(long result) {
        StringBuilder sb = new StringBuilder();
        sb.append(String.valueOf(result >> 24 & 0xFF)).append(".");
        sb.append(String.valueOf(result >> 16 & 0xFF)).append(".");
        sb.append(String.valueOf(result >> 8 & 0xFF)).append(".");
        sb.append(String.valueOf(result & 0xFF));

        return sb.toString();
    }

    /**
     * 获取格式化设备号
     *
     * @param id
     * @return
     */
    public static String getFormartDiNumber(long id) {
        StringBuilder sb = new StringBuilder();
        String tmp = String.valueOf(id >> 24 & 0xFF);
        if (tmp.length() == 1) {
            sb.append("0").append(tmp);
        }else{
        	sb.append(tmp);
        }

        tmp = String.valueOf(id >> 16 & 0xFF);
        if (tmp.length() == 1) {
            sb.append("0").append(tmp);
        }else{
        	sb.append(tmp);
        }

        tmp = String.valueOf(id >> 8 & 0xFF);
        if (tmp.length() == 1) {
            sb.append("0").append(tmp);
        }else{
        	sb.append(tmp);
        }

        tmp = String.valueOf(id & 0xFF);
        if (tmp.length() == 1) {
            sb.append("0").append(tmp);
        }else{
        	sb.append(tmp);
        }

        return sb.toString();
    }
    
    /**
     * smart1的imei规则为
     * @param imei
     * @return
     */
    public static SmartType getSmartType(byte[] byImei){
    	if(byImei == null){
    		throw new IllegalArgumentException();
    	}
    	
    	if(byImei[2] == 0x90){
    		return SmartType.Smart1;
    	}else if(byImei[2] == 0x91){
    		return SmartType.Smart1S;
    	}else{
    		throw new IllegalArgumentException("byImei 不符合规则："+Hex.encodeHexString(byImei));
    	}
    }
}
